package cn.itcast.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfig {

    /**
     * 懒惰队列 磁盘IO - 稳定
     *
     * @return {@link Queue}
     */
    @Bean
    public Queue lazyQueue() {
        return QueueBuilder.durable("lazy.queue")
                .lazy()
                .build();
    }

    @Bean
    public Queue ordinaryQueue() {
        return QueueBuilder.durable("ordinary.queue").build();
    }

    // @Bean
    // public DirectExchange delayedDirectExchange() {
    //     return ExchangeBuilder.directExchange("delayed.direct")
    //             .delayed()
    //             .build();
    // }

    @Bean
    public Queue delayedQueue() {
        return QueueBuilder.durable("delayed.queue").build();
    }

    // @Bean
    // public Binding delayedQueueBindingDelayedDirectExchange() {
    //     return BindingBuilder.bind(delayedQueue())
    //             .to(delayedDirectExchange())
    //             .with("delay");
    // }

    /**
     * ttl队列 设置队列超时时间是10s
     *
     * @return {@link Queue}
     */
    @Bean
    public Queue ttlQueue() {
        return QueueBuilder.durable("ttl.queue")
                .ttl(10000)
                .deadLetterExchange("dlx.direct")
                .deadLetterRoutingKey("dlx")
                .build();
    }

    /**
     * 死信交换
     *
     * @return {@link DirectExchange}
     */
    @Bean
    public DirectExchange deadLetterExchange() {
        return ExchangeBuilder.directExchange("dlx.direct").build();
    }

    @Bean
    public Queue deadLetterQueue() {
        return QueueBuilder.durable("dlx.queue").build();
    }

    @Bean
    public Binding deadLetterQueueBindingDeadLetterExchange() {
        return BindingBuilder.bind(deadLetterQueue())
                .to(deadLetterExchange()).with("dlx");
    }

    // @Bean
    // public Queue simpleQueue() {
    //     return QueueBuilder.durable("simple.queue")
    //             .deadLetterExchange("dlx.direct")
    //             .deadLetterRoutingKey("dlx")
    //             .build();
    // }

    @Bean
    public TopicExchange demoTopicExchange() {
        return ExchangeBuilder
                .topicExchange("demo.topic")
                .build();
    }

    @Bean
    public Queue demoQueue() {
        return QueueBuilder.durable("demo.queue")
                .build();
    }

    @Bean
    public Binding demoQueueBindingDemoTopicExchange() {
        return BindingBuilder.bind(demoQueue())
                .to(demoTopicExchange())
                .with("red.#");
    }


}
